﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace produccion.forms
{
    /// <summary>
    /// Lógica de interacción para FRMFacturacionVarios.xaml
    /// </summary>
    
    public partial class FRMFacturacionVarios : Window
    {
        public FRMFacturacionVarios()
        {
            InitializeComponent();
        }

        private void dtgArticulos_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;

            if (e.Key == Key.Enter)
            {

                string StrDatos1 = "";
                DataGridCell cell = DataGridHelper.GetCell(dtgArticulos, dtgArticulos.SelectedIndex, 0);
                int i = dtgArticulos.SelectedIndex;

                int IntColumna = dtgArticulos.CurrentCell.Column.DisplayIndex;
                StrDatos1 = cell.ToString().Replace("System.Windows.Controls.DataGridCell:", "").Trim();

                //e.Handled = true;
                //elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

                if (cell != null)
                {

                    if (IntColumna == 0)
                    {
                        if (StrDatos1.Length == 10 || StrDatos1.Length == 6 || StrDatos1.Length == 4)
                        {

                            if (i == 0)
                            {
                                e.Handled = true;
                                elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                            }
                            else
                            {
                                dtgArticulos.CurrentCell = new DataGridCellInfo(dtgArticulos.Items[i - 1], dtgArticulos.Columns[2]);
                                dtgArticulos.ScrollIntoView(dtgArticulos.Items[i]);
                                dtgArticulos.BeginEdit();
                            }

                            //dtgArticulos.CurrentCell = new DataGridCellInfo(dtgArticulos.Items[i], dtgArticulos.Columns[2]);
                            //dtgArticulos.BeginEdit();
                        }
                        else
                        {
                            e.Handled = true;
                            elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                        }
                    }
                    else
                    {
                        e.Handled = true;
                        elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                    }
                }
            }

        }

        //
        // SINGLE CLICK EDITING
        //

        private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataGridCell cell = sender as DataGridCell;
            if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
            {
                if (!cell.IsFocused)
                {
                    cell.Focus();
                }
                DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                if (dataGrid != null)
                {
                    if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                    {
                        if (!cell.IsSelected)
                            cell.IsSelected = true;
                    }
                    else
                    {
                        DataGridRow row = FindVisualParent<DataGridRow>(cell);
                        if (row != null && !row.IsSelected)
                        {
                            row.IsSelected = true;
                        }
                    }
                }
            }
        }

        static T FindVisualParent<T>(UIElement element) where T : UIElement
        {
            UIElement parent = element;
            while (parent != null)
            {
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                {
                    return correctlyTyped;
                }

                parent = VisualTreeHelper.GetParent(parent) as UIElement;
            }
            return null;
        }

        private void FacturacionVarios_KeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.F3 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta == true) { MDOgeneral.logBusquedadAbierta = false; }
            }

            if (e.Key == Key.F4 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta1 == true) { MDOgeneral.logBusquedadAbierta1 = false; }
            }

            if (e.Key == Key.F5 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta2 == true) { MDOgeneral.logBusquedadAbierta2 = false; }
            }
        }

        private void txtDetalle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dtgArticulos.Focus();
                dtgArticulos.CurrentCell = new DataGridCellInfo(dtgArticulos.Items[0], dtgArticulos.Columns[0]);
                dtgArticulos.BeginEdit();
            }
        }

        private void FacturacionVarios_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(chkArtesa);
            deshabilitar();
            txtDetalle.Style = null;

            if (MDOgeneral.strEmpresaConectada == "ART")
            {

                chkArtesa.IsChecked = true;
                chkArtesaPele.IsChecked = false;
            }
            else
            {
                chkArtesa.IsChecked = false;
                chkArtesaPele.IsChecked = true;
            }


            #region EstilosParaDataGrid
            Style MyStyleTextAlignmentProperty = new Style(typeof(DataGridCell));
            MyStyleTextAlignmentProperty.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right));

            dtgArticulos.Columns[2].CellStyle = MyStyleTextAlignmentProperty;
            dtgArticulos.Columns[3].CellStyle = MyStyleTextAlignmentProperty;
            dtgArticulos.Columns[4].CellStyle = MyStyleTextAlignmentProperty;
            dtgArticulos.Columns[5].CellStyle = MyStyleTextAlignmentProperty;
            dtgArticulos.Columns[6].CellStyle = MyStyleTextAlignmentProperty;
            #endregion
        }

        private void chkArtesa_Checked(object sender, RoutedEventArgs e)
        {
            if (chkArtesa.IsChecked == true)
            {
                habilitar();
            }
        }

        private void chkArtesaPele_Checked(object sender, RoutedEventArgs e)
        {
            if (chkArtesaPele.IsChecked == true)
            {
                habilitar();
            }
        }

        private void chkNC_Checked(object sender, RoutedEventArgs e)
        {
            if (chkNC.IsChecked == true)
            {
                btnImportarDev.Visibility = Visibility.Visible;
                btnImportarRec.Visibility = Visibility.Visible;
            }
        }

        private void chkFactura_Checked(object sender, RoutedEventArgs e)
        {
            if (chkFactura.IsChecked == true)
            {
                btnImportarDev.Visibility = Visibility.Hidden;
                btnImportarRec.Visibility = Visibility.Hidden;
            }
        }

        private void chkND_Checked(object sender, RoutedEventArgs e)
        {
            if (chkND.IsChecked == true)
            {
                btnImportarDev.Visibility = Visibility.Hidden;
                btnImportarRec.Visibility = Visibility.Hidden;
            }
        }

        private void habilitar()
        {
            try
            {
                chkFactura.IsEnabled = true;
                chkNC.IsEnabled = true;
                chkND.IsEnabled = true;

                txtCliente.IsEnabled = true;
                txtDetalle.IsEnabled = true;
                cmbConcepto.IsEnabled = true;
                dtpFecha.IsEnabled = true;

                btnBuscar.IsEnabled = true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void deshabilitar()
        {
            try
            {
                chkFactura.IsEnabled = false;
                chkNC.IsEnabled = false;
                chkND.IsEnabled = false;

                txtCliente.IsEnabled = false;
                txtDetalle.IsEnabled = false;
                cmbConcepto.IsEnabled = false;
                dtpFecha.IsEnabled = false;

                btnBuscar.IsEnabled = false;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        #region ParaObtenerLasPropiedadesDeLaCeldaDelGrid
        static class DataGridHelper
        {
            static public DataGridCell GetCell(DataGrid dg, int row, int column)
            {
                DataGridRow rowContainer = GetRow(dg, row);

                if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

                    // try to get the cell but it may possibly be virtualized
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    if (cell == null)
                    {
                        // now try to bring into view and retreive the cell
                        dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    }
                    return cell;
                }
                return null;
            }

            static public DataGridRow GetRow(DataGrid dg, int index)
            {
                DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
                if (row == null)
                {
                    // may be virtualized, bring into view and try again
                    dg.ScrollIntoView(dg.Items[index]);
                    row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
                }
                return row;
            }

            static T GetVisualChild<T>(Visual parent) where T : Visual
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                    {
                        child = GetVisualChild<T>(v);
                    }
                    if (child != null)
                    {
                        break;
                    }
                }
                return child;
            }
        }
        #endregion

        

        
    }
}
